# ------------------------------------------------------------------------------
# cnn.py
# shenyczz@163.com
# ------------------------------------------------------------------------------
import torch.nn as nn

class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Sequential(
            # in_channels, out_channels, kernel_size, stride, padding
            nn.Conv2d(1, 16, 5, 1, 2),
            nn.ReLU(),
            nn.MaxPool2d(2), # 28*28 => 14*14
        )
        self.conv2 = nn.Sequential(
            nn.Conv2d(16, 32, 5, 1, 2),
            nn.ReLU(),
            nn.MaxPool2d(2), # 14*14 => 7*7
        )
        # y = xA^T + b
        # 输入的神经元个数 = 7*7*32 表示 32 channels, 每 channel 为 7*7
        self.out = nn.Linear(7*7*32, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = x.view(x.size(0), -1)  # reshape
        out = self.out(x)
        return out, x


